<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />

    <!-- 文章信息区域 -->
    <div class="article-container">
      <!-- 文章标题 -->
      <h1 class="art-title">{{articleObj.title}}</h1>

      <!-- 用户信息 -->
      <van-cell center :title="articleObj.aut_name" :label="relativeTime(articleObj.pubdate)">
        <template #icon>
          <img :src="articleObj.aut_photo" alt="" class="avatar" />
        </template>
        <template #default>
          <div>
            <van-button type="info" size="mini" v-if="articleObj.is_followed" @click="isFollowFn(articleObj.aut_id)">已关注</van-button>
            <van-button icon="plus" type="info" size="mini" plain v-else @click="isFollowFn(articleObj.aut_id)"
              >关注</van-button
            >
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="articleObj.content"></div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box">
        <van-button icon="good-job" type="danger" size="small" v-if="articleObj.attitude === 1" @click="isLikeFn(0)"
          >已点赞</van-button
        >
        <van-button icon="good-job-o" type="danger" plain size="small" v-else @click="isLikeFn(1)"
          >点赞</van-button
        >
      </div>
    </div>

    <!-- 评论区域 -->
    <comment-list-vue :artId="$route.query.id"></comment-list-vue>
  </div>
</template>

<script>
import { relativeTime } from '@/utils/relativeTime'
import { getArticleDetail, setUserFollowings, setCancelUserFollowings, setArticleLikings, setNoArticleLikings } from '@/api'
import CommentListVue from '@/views/ArticleDetail/CommenList.vue'
export default {
  name: 'ArticleDetailVue',
  components: {
    CommentListVue
  },
  data () {
    return {
      articleObj: {} // 文章详细信息
    }
  },
  async created () {
    // 请求文章详细数据
    const res = await getArticleDetail(this.$route.query.id)
    this.articleObj = res.data.data
  },
  methods: {
    // 是否关注作者
    async isFollowFn (id) {
      // 没有关注->关注
      if (!this.articleObj.is_followed) {
        this.articleObj.is_followed = true
        await setUserFollowings(id)
      } else {
        // 关注->取消关注
        this.articleObj.is_followed = false
        await setCancelUserFollowings(id)
      }
    },
    // 是否喜欢文章
    async isLikeFn (id) {
      if (id === 1) {
        this.articleObj.attitude = id
        await setArticleLikings(this.articleObj.art_id)
      } else {
        this.articleObj.attitude = id
        await setNoArticleLikings(this.articleObj.art_id)
      }
    },
    // 格式化时间
    relativeTime
  }
}
</script>

<style scoped lang="less">
.van-nav-bar {
    position: fixed;
}
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
  /deep/ img {
    width: 100%;
  }
  /deep/ pre {
    white-space: pre-wrap;
    word-wrap: break-word;
  }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
</style>
